import 'package:eyepetizer_example/config/string.dart';
import 'package:eyepetizer_example/widget/go_navigation.dart';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';

import '../../config/color.dart';
import '../../model/discovery/topic_detail_model.dart';
import '../../page/video/video_detail_page.dart';
import '../../utils/cache_image.dart';
import '../../utils/date_util.dart';
import '../expand_more_text_widget.dart';

class TopicDetailItemWidget extends StatelessWidget{
  final TopicDetailItemData model;
  const TopicDetailItemWidget({Key? key, required this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        var data = model.data?.content?.data;
        if(data == null) {
          return;
        }
        GoNavigator.toPage(context, VideoDetailPage(data));
      },
      child: Column(
        children: [
          // author 介绍
          _author(),
          // 作品描述
          _description(),
          // 标签
          _tag(),
          // 视频图片
          _videoImage(context),
          // 视频状态
          _videoState(),
          // 分割线
          _divider(),
        ],
      ),
    );
  }

 Widget _author() {
    return Row(
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.fromLTRB(20, 20, 10, 0),
          child: ClipOval(
            child: cacheImage(
              model.data?.header?.icon??"",
              width: 45,
              height: 45,
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: const EdgeInsets.fromLTRB(0, 20, 20, 0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                   model.data?.header?.issuerName??"",
                  style: const TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 14),
                ),
                // 作品发布时间
                Row(
                  children: <Widget>[
                    Text(
                      '${formatDateMsByYMD(model.data?.header?.time??0)}发布：',
                      style:
                      const TextStyle(color: AppColor.hitTextColor, fontSize: 12),
                    ),
                    Expanded(
                      child: Text(
                        model.data?.content?.data?.title??"",
                        style: const TextStyle(
                            color: Colors.black,
                            fontSize: 12,
                            fontWeight: FontWeight.bold),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  /// 作品描述
  _description() {
    var textStyle = const TextStyle(
        fontSize: 12, color: Colors.blue, fontWeight: FontWeight.bold);
    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
      child: ExpandMoreTextWidget(
        model.data?.content?.data?.description??"",
        style: const TextStyle(fontSize: 14, color: AppColor.desTextColor),
        moreStyle: textStyle,
        lessStyle: textStyle,
      ),
    );
  }

  /// 标签
  Widget _tag() {
    return Padding(padding: const EdgeInsets.fromLTRB(20, 0, 20, 10),
      child: Row(
        children: _getTagWidgetList(model),
      ),
    );
  }

  /// 视频图片
  Widget _videoImage(BuildContext context) {
    var time = model.data?.content?.data?.duration;
    var formatTime = time != null ? formatDateMsByMS(time * 1000) : 0;
    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 0, 20, 5),
      child: Stack(
        children: <Widget>[
          ClipRRect(
              borderRadius: BorderRadius.circular(4),
              child: Hero(
                tag:
                '${model.data?.content?.data?.id}${model.data?.content?.data?.time}',
                child: cacheImage(
                  model.data?.content?.data?.cover?.feed??"",
                  width: MediaQuery.of(context).size.width,
                  height: 200,
                ),
              )),
          Positioned(
            right: 8,
            bottom: 8,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: Container(
                decoration: const BoxDecoration(color: Colors.black54),
                padding: const EdgeInsets.all(5),
                child: Text(
                  "$formatTime",
                  style: const TextStyle(
                      color: Colors.white,
                      fontSize: 12,
                      fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 视频状态
  Widget _videoState() {
    return Padding(
      padding: const EdgeInsets.only(left: 20, right: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // 点赞
          Row(
            children: [
              const Icon(Icons.favorite_border, size: 20, color: AppColor.hitTextColor,),
              Padding(
                padding: const EdgeInsets.only(left: 10),
                child: Text(
                  "${model.data?.content?.data?.consumption?.collectionCount}",
                  style: const TextStyle(
                    fontSize: 12,
                    color: AppColor.hitTextColor
                  ),
                ),
              ),
            ],
          ),
          //评论
          Row(
            children: [
              const Icon(Icons.message, size: 20, color: AppColor.hitTextColor,),
              Padding(
                padding: const EdgeInsets.only(left: 10),
                child: Text(
                  "${model.data?.content?.data?.consumption?.replyCount}",
                  style: const TextStyle(
                    fontSize: 12,
                    color: AppColor.hitTextColor
                  ),
                ),
              )
            ],
          ),
          //收藏
          Row(
            children: const [
              Icon(Icons.star_border, size: 20, color: AppColor.hitTextColor,),
              Padding(
                padding: EdgeInsets.only(left: 10),
                child: Text(
                  AppString.collect_text,
                  style: TextStyle(
                    fontSize: 12,
                    color: AppColor.hitTextColor
                  ),
                ),
              )
            ],
          ),
          //分享
          Row(
            children: [
              IconButton(
                icon: const Icon(Icons.share, color: AppColor.hitTextColor,),
                onPressed: (){
                  var shareString = "${model.data?.content?.data?.title}";
                  Share.share(shareString);
                },
              )
            ],
          )
        ],
      ),
    );
  }

  Widget _divider() {
    return const Padding(
      padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
      child: Divider(
        height: 0.5,
      ),
    );
  }

  List<Widget> _getTagWidgetList(TopicDetailItemData model) {
    var widgetList = model.data?.content?.data?.tags?.map((tag) =>
        Container(
          margin: const EdgeInsets.only(right: 5),
          alignment: Alignment.center,
          height: 20,
          padding: const EdgeInsets.only(left: 5, right: 5),
          decoration: BoxDecoration(
              color: AppColor.tabBgColor, borderRadius: BorderRadius.circular(4)),
          child: Text(
            tag.name??"",
            style: const TextStyle(fontSize: 12, color: Colors.blue),
          ),
        )
    ).toList();

    if(widgetList == null) {
      return [];
    }
    return widgetList.length > 3 ? widgetList.sublist(0, 3) : widgetList;
  }
}

